package com.huadao.cart_ccs.experiment.sample;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.ExperimentStageManager;
import com.huadao.cart_ccs.common.ProjectManager;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.common.adapter.CommonRecycleAdapter;
import com.huadao.cart_ccs.common.adapter.CommonViewHolder;
import com.huadao.cart_ccs.common.adapter.FooterHolder;
import com.huadao.cart_ccs.common.adapter.HeaderHolder;
import com.huadao.cart_ccs.common.dialog.DialogFragmentHelper;
import com.huadao.cart_ccs.common.dialog.IDialogResultListener;
import com.huadao.cart_ccs.greendao.entity.Sample;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

//取样
public class SampleDF extends BaseDialogFragment implements SampleContract.ISampleView {

    @BindView(R.id.btn_start)
    Button btn_start;
    @BindView(R.id.btn_finish)
    Button btn_finish;
    @BindView(R.id.btn_save)
    Button btn_save;
    @BindView(R.id.btn_sample)
    Button btn_sample;
    @BindView(R.id.sp_sample_count)
    Spinner spSampleCount;
    @BindView(R.id.et_sample_cell_amount)
    EditText etSampleCellAmount;
    @BindView(R.id.et_mainTank_cellFluid)
    EditText etMainTankCellFluid;
    @BindView(R.id.et_density_before_adjust)
    EditText etDensityBeforeAdjust;
    @BindView(R.id.et_density_after_adjust)
    EditText etDensityAfterAdjust;
    @BindView(R.id.tv_operate_date)
    TextView tvOperateDate;
    @BindView(R.id.tv_operate_time)
    TextView tvOperateTime;
    @BindView(R.id.tv_sample_people)
    TextView tvSamplePeople;
    @BindView(R.id.tv_history_record)
    TextView tvHistoryRecord;

    private OnSample onSample;

    private SamplePresenter presenter;

    private Date date;

    public interface OnSample {
        void sampleStart();

        void sampleFinish();

        void saveSampleData(Sample sample);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sample;
    }

    @Override
    protected void onInit() {
        this.setCancelable(false);
        presenter = new SamplePresenter(this);
        date = new Date();

        tvOperateDate.setText(TimeUtil.date_sdf.format(date));
        tvOperateTime.setText(TimeUtil.time_sdf.format(date));
        tvSamplePeople.setText(CartApplication.getInstance().getOperator());
//        presenter.getHistorySampleList();
    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @Override
    public void onGetHistorySampleList(List<Sample> sampleList) {
        if(sampleList==null||sampleList.size()==0){
            tvHistoryRecord.setVisibility(View.GONE);
            return;
        }
        for(Sample sample:sampleList){
            String text=String.format("%s于%tF %tT进行了取样操作，取样类型为%s;数据——计数细胞量%d个、主罐体细胞液 %dml、调整前密度 %.2f%%、调整后密度 %.2f%%",sample.getOperator(),sample.getDate(),sample.getDate(),sample.getSampleCount(),sample.getCellAmount(),sample.getMainTankCellFluid(),sample.getDensityBeforeAdjust(),sample.getDensityAfterAdjust());
            tvHistoryRecord.setVisibility(View.VISIBLE);
            tvHistoryRecord.append(text);
        }
    }

    public void setOnSample(OnSample onSampleBack) {
        this.onSample = onSampleBack;
    }

    @OnClick({R.id.btn_close, R.id.btn_start, R.id.btn_finish, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                this.dismiss();
                break;
            case R.id.btn_start:
                DialogFragmentHelper.showConfirmDialog(getActivity(), getString(R.string.startSample), "执行此操作需运行循环泵，才能进行取样操作！", new IDialogResultListener<Integer>() {
                    @Override
                    public void onDataResult(Integer result) {
                        if (result == DialogInterface.BUTTON_POSITIVE) {
                            startSample();
                        }
                    }
                },true);
                break;
            case R.id.btn_finish:
                finishSample();
                btn_finish.setVisibility(View.GONE);
                btn_save.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_save:
                saveSample();
                this.dismiss();
                break;
        }
    }

    private void startSample() {
        if (onSample != null) {
            onSample.sampleStart();
        }
    }

    private void finishSample() {
        if (onSample != null) {
            onSample.sampleFinish();
        }
    }

    private void saveSample() {
        String err = checkParams();
        if (err != null) {
            showToast(err);
            return;
        }
        if (onSample != null) {
            onSample.saveSampleData(generateSampleData());
        }
    }

    private Sample generateSampleData() {
        Sample sample=new Sample();
        sample.setSampleId(null);
        sample.setProjectId(ProjectManager.getInstance().getProjectId());
        sample.setStageId(ExperimentStageManager.getInstance().getStageId());
        sample.setSampleCount(spSampleCount.getSelectedItem().toString());
        sample.setCellAmount(Integer.parseInt(etSampleCellAmount.getText().toString()));
        sample.setMainTankCellFluid(Integer.parseInt(etMainTankCellFluid.getText().toString()));
        sample.setDensityBeforeAdjust(Double.parseDouble(etDensityBeforeAdjust.getText().toString()));
        sample.setDensityAfterAdjust(Double.parseDouble(etDensityAfterAdjust.getText().toString()));
        sample.setDate(date);
        sample.setOperator(tvSamplePeople.getText().toString());
        return sample;
    }

    private String checkParams() {
        String errr = null;
        if (TextUtils.isEmpty(spSampleCount.getSelectedItem().toString())) {
            errr = "请输入取样计数";
        }else if (TextUtils.isEmpty(etSampleCellAmount.getText().toString())) {
            errr = "请输入计数细胞量";
        }else if (TextUtils.isEmpty(etMainTankCellFluid.getText().toString())) {
            errr = "请输入主罐体细胞液";
        }else if (TextUtils.isEmpty(etDensityBeforeAdjust.getText().toString())) {
            errr = "请输入调整前密度";
        }else if (TextUtils.isEmpty(etDensityAfterAdjust.getText().toString())) {
            errr = "请输入调整后密度";
        } else if (date == null) {
            errr = "请输入取样时间";
        }else if (TextUtils.isEmpty(tvSamplePeople.getText().toString())) {
            errr = "请输入取样人";
        }
        return errr;
    }

    public void startSampleOK() {
        btn_start.setVisibility(View.GONE);
        btn_finish.setVisibility(View.VISIBLE);
    }

}
